<template>
	<div>
		<el-dialog :title="$t('action.add')" width="70%" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form" :model="form" :rules="rules" :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">
 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.OtherPiece_userName')" prop="userName">
							<el-input style="width: 225px"
                        v-model="form.userName" autoComplete="off" clearable
                        :placeholder="$t('system.pleaseInput',{title:$t('field.OtherPiece_userName')})">
							</el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.OtherPiece_materialNo')" prop="materialNo">
              <el-select style="width: 225px" filterable remote size="small" :remote-method="e => getMaterialStockList(e)" v-model="form.materialNo" clearable class="material" placeholder="请输入5位以上的物料编码/物料名称">
                <el-option v-for="item in this.materialList" :key="item.fnumber" :label="item.fnumber + '：' + item.fname + ' ' + item.fspecification" :value="item.fnumber" />
              </el-select>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.OtherPiece_count')" prop="count">
							<el-input style="width: 225px"
                        v-model="form.count" autoComplete="off" clearable
                        type="text"
                        onkeyup="value=value.replace(/[^\d.]/g, '')"
                        :placeholder="$t('system.pleaseInput',{title:$t('field.OtherPiece_count')})">
							</el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.OtherPiece_workTime')" prop="createTime">
              <el-date-picker
                  v-model="form.workTime"
                  type="datetime"
                  placeholder="选择日期">
              </el-date-picker>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.OtherPiece_piece')" prop="piece">
							<el-input  style="width: 225px" v-model="form.piece" autoComplete="off" clearable
                        type="text"
                        onkeyup="fvalue=value.replace(/[^\d.]/g, '')"
                        :placeholder="$t('system.pleaseInput',{title:$t('field.OtherPiece_piece')})">
							</el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
				 </el-row>
				</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">{{$t('system.confirm')}}</span>
					<span v-else>{{$t('system.submitting')}}</span>
				</el-button>
				<el-button :size="size" @click="closeForm">{{$t('system.cancel')}}</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { add } from '@/api/app/otherpiece'
import {getMaterialByKey} from "@/api/app/material";



export default {
	name:'otherPieceadd',
	components: {
	   	   
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
		querys:{
			type:Object,
		},
	},
	data(){
    var countRules = (rule, value, callback) => {
      if ((String(value).length - (String(value).indexOf(".") + 1)) > 2 && String(value).indexOf(".") >= 0) {
        callback(new Error("限制两位小数"));
      } else {
        callback();
      }
    }

		return {
		    dialog:{
			},
			
			form: {
			   userName:'',
			   userId:'',
			   materialNo:'',
			   materialName:'',
			   count:'',
			   createTime:'',
			   piece:'',
			   totalCount:'',
         workTime:''
			},
		    initial:{},   
			loading:false,
			rules: {
        count: [{ validator: countRules,  trigger: 'blur'} ],
        piece: [{ validator: countRules,  trigger: 'blur'} ],

			},
      materialList:[],
		}
	},
	watch:{
		show(val){
			if(val){
			}
		}
	},
	methods: {
    /**
     * 获取物料信息
     * @param val
     */
    getMaterialStockList(val){
      if (val.length < 3) {
        this.materialList = []
        return
      }
      if (!val) {
        this.materialList = []
        return
      }
      try {
        getMaterialByKey({key:val}).then(res => {
          this.materialList = res.data.data.data
        })

      } finally {

      }
    },
		open(){
		  if(this.querys){
		  }
		},
		submit(){
			this.$refs['form'].validate(valid => {
				if(valid) {
					this.loading = true
					add(this.form).then(res => {
						if(res.status == 200){
							this.$message({message: this.$t('system.success'), type: 'success'})
							this.$emit('refresh_list')
							this.closeForm()
						}
					}).catch(()=>{
						this.loading = false
					})
				}
			})
		},
		closeForm(){
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form']!==undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common.scss';
</style>
